package com.otitan.la.forest.industry.client.http;

import com.otitan.la.forest.industry.client.http.methods.AbstractHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: HttpClient
 * @Author: jiachangsen
 * @CreateDate: 2020/10/15 11:31
 * @UpdateUser: jiacs
 * @UpdateDate: 2020/10/15 11:31
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class HttpPostForm extends AbstractHttpClient {

    public HttpPostForm(String path, String token,Object params) {
        super(path, token, params);
    }

    @Override
    public void execute() {
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        if (params instanceof Map){
            Map<String, Object> map= (Map)params;
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }

        HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8);
        postRequest(CONTENT_TYPE_FORM_DATA, entity);
    }
}
